using System;
using System.Collections;
using System.Resources;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using newtelligence.DasBlog.Web.Core;

namespace newtelligence.DasBlog.Web
{
    public class StatisticsItem
    {
        internal int count;
        internal string identifier;

        internal StatisticsItem(string identifier, int count)
        {
            this.identifier = identifier;
            this.count = count;
        }

        #region Nested type: Comparer

        public class Comparer : IComparer
        {
            #region IComparer Members

            public int Compare(object left, object right)
            {
                StatisticsItem leftItem = (StatisticsItem) left;
                StatisticsItem rightItem = (StatisticsItem) right;

                int leftValue = leftItem.count;
                int rightValue = rightItem.count;
                if (leftValue > rightValue)
                {
                    return -1;
                }
                else if (leftValue < rightValue)
                {
                    return 1;
                }

                return
                    String.Compare(leftItem.identifier, rightItem.identifier,
                                   StringComparison.InvariantCultureIgnoreCase);
            }

            #endregion
        }

        #endregion

        #region Nested type: SearchStringComparer

        public class SearchStringComparer : IComparer
        {
            #region IComparer Members

            public int Compare(object left, object right)
            {
                StatisticsItem leftItem = (StatisticsItem) left;
                StatisticsItem rightItem = (StatisticsItem) right;

                int leftValue = leftItem.count;
                int rightValue = rightItem.count;
                if (leftValue > rightValue)
                {
                    return -1;
                }
                else if (leftValue < rightValue)
                {
                    return 1;
                }

                return
                    String.Compare(SiteUtilities.ParseSearchString(leftItem.identifier).Text,
                                   SiteUtilities.ParseSearchString(rightItem.identifier).Text,
                                   StringComparison.InvariantCultureIgnoreCase);
            }

            #endregion
        }

        #endregion
    }

    public delegate void StatisticsBuilderCallback(TableRow currentRow, StatisticsItem item, object args);

    public class StatisticsListBase : UserControl
    {
        protected ResourceManager resmgr;

        public StatisticsListBase()
        {
            resmgr = ApplicationResourceTable.Get();
        }

        protected Table BuildStatisticsTable(
            IEnumerable items,
            string itemColumnName,
            string numberColumnName,
            StatisticsBuilderCallback buildFn,
            object args)
        {
            string total;
            return BuildStatisticsTable(items, itemColumnName, numberColumnName, buildFn, out total, args);
        }

        protected Table BuildStatisticsTable(
            IEnumerable items,
            string itemColumnName,
            string numberColumnName,
            StatisticsBuilderCallback buildFn,
            out string totalString,
            object args)
        {
            int total = 0;
            Table table = new Table();
            table.CssClass = "statsTableStyle";
            TableRow row = new TableRow();
            row.CssClass = "statsTableHeaderRowStyle";
            row.Cells.Add(new TableCell());
            row.Cells.Add(new TableCell());
            row.Cells[0].CssClass = "statsTableHeaderColumnStyle";
            row.Cells[1].CssClass = "statsTableHeaderNumColumnStyle";
            row.Cells[0].Text = itemColumnName;
            row.Cells[1].Text = numberColumnName;
            table.Rows.Add(row);

            foreach (StatisticsItem item in items)
            {
                row = new TableRow();
                row.CssClass = "statsTableRowStyle";
                row.Cells.Add(new TableCell());
                row.Cells.Add(new TableCell());
                row.Cells[0].CssClass = "statsTableColumnStyle";
                row.Cells[1].CssClass = "statsTableNumColumnStyle";

                buildFn(row, item, args);

                if (row.Cells[1].Text.Length > 0)
                {
                    total += item.count;
                    table.Rows.Add(row);
                }
            }

            TableRow totalRow = new TableRow();
            totalRow.CssClass = "statsTableFooterRowStyle";
            totalRow.Cells.Add(new TableCell());
            totalRow.Cells.Add(new TableCell());
            totalRow.Cells[0].CssClass = "statsTableFooterColumnStyle";
            totalRow.Cells[1].CssClass = "statsTableFooterNumColumnStyle";
            totalRow.Cells[0].Text = resmgr.GetString("text_activity_total");
            totalString = total.ToString();
            totalRow.Cells[1].Text = totalString;
            table.Rows.Add(totalRow);
            return table;
        }

        protected static void BuildAgentsRow(TableRow row, StatisticsItem item, object unused)
        {
            Label agentLabel = new Label();
            string text = SiteUtilities.ClipString(item.identifier, 80);
            agentLabel.Text = text;
            row.Cells[0].Controls.Add(agentLabel);
            row.Cells[1].Text = item.count.ToString();
        }

        protected static void BuildReferrerRow(TableRow row, StatisticsItem item, object unused)
        {
            HyperLink link = new HyperLink();
            string text = SiteUtilities.ClipString(item.identifier, 80);
            link.Text = HttpUtility.HtmlEncode(text);
            link.NavigateUrl = item.identifier;
            row.Cells[0].Controls.Add(link);
            row.Cells[1].Text = item.count.ToString();
        }

        protected static void BuildSearchesRow(TableRow row, StatisticsItem item, object unused)
        {
            HyperLink link = SiteUtilities.ParseSearchString(item.identifier);

            if (link != null)
            {
                row.Cells[0].Controls.Add(link);
                row.Cells[1].Text = item.count.ToString();
            }
        }

        protected static void BuildUserDomainRow(TableRow row, StatisticsItem item, object unused)
        {
            Label userDomainLabel = new Label();
            string text = SiteUtilities.ClipString(item.identifier, 80);
            userDomainLabel.Text = text;
            row.Cells[0].Controls.Add(userDomainLabel);
            row.Cells[1].Text = item.count.ToString();
        }

        protected static ArrayList GenerateSortedItemList(IDictionary dict)
        {
            ArrayList listItems = new ArrayList(dict.Count);

            foreach (DictionaryEntry de in dict)
            {
                listItems.Add(new StatisticsItem(de.Key.ToString(), (int) de.Value));
            }

            listItems.Sort(new StatisticsItem.Comparer());
            return listItems;
        }

        protected static ArrayList GenerateSortedSearchStringItemList(IDictionary dict)
        {
            ArrayList listItems = new ArrayList(dict.Count);

            foreach (DictionaryEntry de in dict)
            {
                listItems.Add(new StatisticsItem(de.Key.ToString(), (int) de.Value));
            }

            listItems.Sort(new StatisticsItem.SearchStringComparer());
            return listItems;
        }
    }
}